/*
 * Package il.ac.biu.cs.grossmm.impl.server
 * File WrappedImpl.java
 * 
 * This is a part of presence service framework prototype. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package il.ac.biu.cs.grossmm.impl.server;

import il.ac.biu.cs.grossmm.api.server.Component;
import il.ac.biu.cs.grossmm.api.server.Loader;
import il.ac.biu.cs.grossmm.api.server.NoSuchComponentException;
import il.ac.biu.cs.grossmm.api.server.SelfLoader;
import il.ac.biu.cs.grossmm.api.server.Service;
import il.ac.biu.cs.grossmm.api.server.Wrapped;
import il.ac.biu.cs.grossmm.api.server.Wrapper;

public class WrappedImpl implements Wrapped {
	final Object object;
	Loader loader;
	final Wrapper wrapper;
	
	final ComponentManagerImpl componentManager;
	final ServiceManagerImpl serviceManager;
	
	private boolean initialized = false;
	
	public WrappedImpl(Object object, Loader loader, Wrapper wrapper) {
		this.object = object;
		this.loader = loader;
		this.wrapper = wrapper;
		
		if( object instanceof Component) 
			componentManager = new ComponentManagerImpl(this);
		else
			componentManager = null;
		
		if( object instanceof Service ) 
			serviceManager = new ServiceManagerImpl(this);
		else
			serviceManager = null;
	}
	
	public void initialize() throws Exception {
		if( initialized )
			return;
        
        initialized = true;
		
		if( object instanceof SelfLoader )
			setLoader(((SelfLoader) object).getLoader(loader));
		
        // initialize component manager (for components)
		if( componentManager != null )
			componentManager.initialize();
		
		// initialize component manager (for services)
		if( serviceManager != null )
			serviceManager.initialize();
        
        
	}
	
	public Object getObject() {
		return object;
	}
	
	ComponentManagerImpl getComponentManager() {
		return componentManager;
	}
	
	ServiceManagerImpl getServiceManager() {
		return serviceManager;
	}
	
	WrappedImpl load(Object hint) throws Exception {
		WrappedImpl w = (WrappedImpl) loader.load(wrapper, hint);
        
        if( w == null )
            throw new NoSuchComponentException(hint.toString());
		
        w.initialize();
        
		return w;
	}

	public Loader getLoader() {
		return loader;
	}

	public void setLoader(Loader loader) {
		if( initialized )
			throw new IllegalStateException();
		
		this.loader = loader;
	}

	public void unload(Wrapped wrapped) {
		// TODO Auto-generated method stub
		
	}
}
